package com.example.robertluozizhao.framecollectdemo.util

import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.example.robertluozizhao.framecollectdemo.R

/**
 * <pre>
 *     author: luozz@anjiu-tech.com
 *     time  : 2020/12/23
 *     desc  :
 * </pre>
 */
class GlobalFloatManager {

    private var windowManager: WindowManager? = null
    private var view: View? = null
    private var isShow = true;

    companion object{
        val INSTANCE:GlobalFloatManager by lazy {
            GlobalFloatManager()
        }
    }

    fun showFloatView(context: Context){
        windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        hideFloatView()
        if (isShow) {
            view = LayoutInflater.from(context).inflate(R.layout.view_global_float, null, false)
            val windowParams = WindowManager.LayoutParams()
            windowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION
            windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            windowParams.gravity = Gravity.START or Gravity.TOP
            windowParams.format = PixelFormat.RGBA_8888
            windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT
            windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            windowManager?.addView(view,windowParams)
        }
    }

    fun hideFloatView(){
        view?.let {
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                if (view!!.isAttachedToWindow) {
                    windowManager?.removeViewImmediate(view)
                }
            }
            view = null
        }
    }

    fun setFloatViewStatus(isShow: Boolean){
        this.isShow = isShow
    }


}